package com.zznzzn.myweb.service;

import com.zznzzn.myweb.exceptions.ServiceException;
import com.zznzzn.myweb.Properties.MyProperties;
import com.zznzzn.myweb.mapper.UserMapper;
import com.zznzzn.myweb.pojo.User;
import com.zznzzn.myweb.util.Base64Util;
import com.zznzzn.myweb.util.JWTUtil;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private MyProperties properties;

    @Override
    public boolean checkUsername(String username) {

        if (mapper.queryUserByName(username)!= null){
            return true;
        }
        return false;
    }

    @Override
    public User login(User user) throws ServiceException {
        User u = mapper.queryUserByName(user.getUsername());

        if (u != null){
            if (BCrypt.hashpw(user.getPassword(),u.getSalt()).equals(u.getPassword())){
                String token = JWTUtil.sign(
                        u.getUuid(),u.getSalt(),
                        new Date(System.currentTimeMillis()+properties.getJwt_exp_time()));
                u.setToken(token);
                int i = mapper.updateUserToken(token, u.getUuid());
                if (i!=1){
                    throw new ServiceException("更新token失败");
                }
                u.setPassword("");
                u.setSalt("");

                return u;
            }else{
                throw new ServiceException("密码错误");
            }

        }else{
            throw new ServiceException("该账号不存在");
        }

    }

    @Override
    public User addUser(User user) throws ServiceException {
        String uuid = Base64Util.newUUID();
        String salt = BCrypt.gensalt(); //29位
        String password = BCrypt.hashpw(user.getPassword(),salt); //60位
        Date now = new Date();
        Date exp = new Date(System.currentTimeMillis()+properties.getJwt_exp_time());
        String token = JWTUtil.sign(uuid,salt,exp);

        user.setUuid(uuid);
        user.setSalt(salt);
        user.setPassword(password);
        user.setCreatetime(now);
        user.setToken(token);

        int i = mapper.addUser(user);
        if (i!=0){
            user.setPassword("");
            user.setSalt("");
            return user;
        }else{
            throw new ServiceException("创建用户失败");
        }
    }

    @Override
    public User getUserById(String uuid) throws ServiceException {
        User u = mapper.queryUserById(uuid);
        if (u == null){
            throw new ServiceException("该账号不存在");
        }else{
            return u;
        }
    }

    @Override
    public User getUserByName(String name) throws ServiceException {

        User u = mapper.queryUserByName(name);
        if (u == null){
            throw new ServiceException("该账号不存在");
        }else{
            return u;
        }
    }

    @Override
    public List<User> listAllUser() {
        return mapper.queryAllUsers();
    }

    @Override
    public boolean updateUser(User user) {
        if (mapper.updateUserProfile(user) != 0){
            return true;
        }else{
            throw new ServiceException("更新失败");
        }
    }

    @Override
    public User harmless(User user) {
        user.setPassword("");
        user.setSalt("");
        user.setToken("");
        return user;
    }
}
